import axios from 'axios'

// 创建axios实例
const api = axios.create({
  baseURL: '/api',
  timeout: 10000
})

// 请求拦截器
api.interceptors.request.use(
  config => {
    const token = localStorage.getItem('adminToken')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    if (error.response?.status === 401) {
      localStorage.removeItem('adminToken')
      localStorage.removeItem('level')
      window.location.href = '/login'
    }
    return Promise.reject(error)
  }
)

// 管理员登录
export const adminLogin = (data) => {
  return api.post('/admin/login', data)
}

// 创建管理员（超级管理员）
export const createAdmin = (data) => {
  return api.post('/admin-manage/create', data)
}

// 更新管理员
export const updateAdmin = (data) => {
  return api.put('/admin-manage/update', data)
}

// 删除管理员
export const deleteAdmin = (adminId) => {
  return api.delete(`/admin-manage/delete/${adminId}`)
}

// 批量删除管理员
export const batchDeleteAdmin = (adminIds) => {
  return api.delete('/admin-manage/batch-delete', { data: adminIds })
}

// 分页查询管理员
export const getAdminPage = (queryDTO, current, size) => {
  return api.post('/admin-manage/page', queryDTO, {
    params: { current, size }
  })
}

// 获取管理员详情
export const getAdminDetail = (adminId) => {
  return api.get(`/admin-manage/detail/${adminId}`)
}

// 获取管理员列表
export const getAdminList = () => {
  return api.get('/admin-manage/list')
}

// 获取医院列表
export const getHospitalList = () => {
  return api.get('/doctor/hospitals')
}

// ==================== 医生报名审核功能 ====================

// 获取活动报名医生列表  
export const getDoctorRegistrationsByActivity = (activityId, params = {}) => {
  return api.get(`/admin/doctor-registration/activity/${activityId}`, { 
    params: {
      current: params.current || 1,
      size: params.size || 10
    }
  })
}

// 审核医生报名并给予积分
export const approveDoctorRegistration = (registrationId, data) => {
  return api.post(`/admin/doctor-registration/approve/${registrationId}`, null, {
    params: {
      status: data.status,
      pointsEarned: data.pointsEarned
    }
  })
}

// 批量审核医生报名
export const batchApproveDoctorRegistrations = (data) => {
  return api.post('/admin/doctor-registration/approve/batch', data)
}

// ==================== 活动报名管理功能（新接口） ====================

/**
 * 获取活动的报名列表（管理员查看）
 * @param {number} activityId - 活动ID
 * @returns {Promise} 报名列表
 */
export const getActivityRegistrations = (activityId) => {
  return api.get(`/activity-registration/activity/${activityId}/list`)
}

/**
 * 获取活动报名统计信息（管理员查看）
 * @param {number} activityId - 活动ID
 * @returns {Promise} 统计信息
 */
export const getActivityRegistrationStatistics = (activityId) => {
  return api.get(`/activity-registration/activity/${activityId}/statistics`)
} 